<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";



class Cmail_groups extends IPlugin 
{
    var $color;
    var $from_email;
    
    function Cmail_groups($parent = null)
    {
        $core = & getCoreInstance();
        $this->from_email= $core->config['system']['info_email'];
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }
    
    function getPluginTag() { return "mail_groups"; }
    function getPluginName() { return "mail_groups Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    function getFromEmail() { return $this->from_email; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuTitle" : { return "Mail groups"; } break;
        };
        return $result;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------
    function doAction($params)
    {
        $core = & getCoreInstance();
        $db = $core->getProperty("db");
        if ($_GET[action]=="doSubmit") 
        {
            //debug($_POST);debug($_GET);
            $is_active=0; if (isset($_POST[is_active])) $is_active=1; 
            $db->Exec("update mail_groups set name='".str2db($_POST[name])."', is_active='$is_active' where group_id=$_GET[id]");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (mail_groupss) : item (id=\"$_GET[id]\") was successfully submited</font><br>";
        }
        if ($_GET[action]=="doAdd")  
        {
            $id=$db->Exec("insert into mail_groups (name) values('".str2db($_POST['name'])."')");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (mail_groupss) : item (id=\"$id\") was successfully added</font><br>"; 
        }
        if ($_GET[action]=="doDelete")  
        {
            //debug($_POST);debug($_GET);
            $db->Exec("delete from mail_groups where group_id=$_GET[id]");
            $GLOBALS[result]="<br>&nbsp;&nbsp;&nbsp;&#8226;&nbsp;&nbsp;<font color=\"green\">Result (mail_groupss) : item (id=\"$_GET[id]\") was successfully removed</font><br>";
        }
        return true;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";

        switch ($_G["page"]) {
            default:
                $page = $this->renderMailList($render);
                break;
        }
        return $render->fetch("file:".$this->tplfolder."/".$page);
    }

    // ----------------------------------------------------------------------------------
    // --- [ renderMailList ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function renderMailList(& $render) 
    {
        $core = & getCoreInstance();
        $db = $core->getProperty("db");
        $pg = & $core->getSnippet("paginator");
        $tmp_url = $this->getURL();
        $page = $ppg = -1; /// curent count page
        $params = array ("cb_class" => & $this, "cb_func" => pg_mail_groups, "urlbase" => $tmp_url, "caption" => "Mail groups", "emptymsg" => "<center>No subscribers mail groups created</center>", "header" => array ("ID", "Mail Group Title", "Active", "Actions"), "items" => "", "source" => "sql", // -- sql, array
        "sql_items" => "select group_id, name, is_active from mail_groups order by group_id asc", "sql_count" => "select count(*) from mail_groups order by group_id asc", "ippg" => 10, // -- items per page
        "nppv" => 5, // -- number pages per view
           "page" => & $page, "ppg" => & $ppg,);
        $HTML = $pg->Render($params);
        $render->assign("mail_groups", $HTML);
        //-----------------------------------------------
        if (isset($GLOBALS[result_faq]))
        {
            $render->assign("result", $GLOBALS[result]);
            unset($GLOBALS[result_faq]);
        }
                return "mail_groups.tpl.php";     
    }
         function pg_mail_groups($data) {
        
            $st_paginator_page = $_REQUEST['st_paginator_page'];
            $st_paginator_ppv = $_REQUEST['st_paginator_ppv'];
            $id=$data[group_id];
            $dt = array (
                array ("<form name=\"form".$data["group_id"]."\" action=\"?path=customerslist,mail_groups&action=doSubmit&id={$id}&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\" method=\"POST\" >".$data["group_id"], "align='center' "),
                array ("<input type=\"text\" size=\"66\" name=\"name\" value=\"".db2str($data["name"])."\">", ""),
                array ("<input type='checkbox' name='is_active' ".($data["is_active"]==1 ? "checked" : "")." /> </form>", "align='center' "),
                array ("<a style=\"cursor:hand\" title=\"click for submit this line\" onclick=\"document.forms['form".$data["group_id"]."'].submit();\">apply</a>&nbsp;<a href=\"?".$this->getURL()."&action=doDelete&id={$id}&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\" title=\"click for delete this line\">delete</a>", "align=\"center\"")  
                       );
            return $dt;
        }
    
}    
    